var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var wxUtil = require('../../utils/wxUtil.js');
var shopInfo = require('../../components/shopInfo/index.js');

Page({
  data: {
    shop: {},
    address: {},
    orderGoods: {},
    goodsList: [],
    orderAmount: 0,
    orderType: '',
    modalHidden: true,
    myIf: false,
    // modalHidden1: true,
    myHidden: false,
    goodsId: "",
    addressId: "",
    province: "",
    city: "",
    county: "",
    provinceId: "",
    cityId: "",
    countyId: "",
    orderId: "",
    wxPayParams: "",
    wxPaySucUrl: "",
    dataLoaded: false,
  },
  /**
   * 页面初始化数据
   */
  showSalt: function (addressId) {
    var $this = this;
    var goodsId = this.data.goodsId;
    var skuId = this.data.skuId;
    var amount = this.data.amount;

    ajax.P(
      '/openapi/CHome/showQuickBuy',
      {
        goodsId: goodsId,
        skuId: skuId,
        amount: amount
      },
      function (data) {
        shopInfo.init('shopInfoData', $this);
        $this.setData({
          orderType: data.result.orderType,
          address: data.result.address,
          goodsList: data.result.goodsList,
          addressId: data.result.address.id,
          orderAmount: data.result.orderAmount,
          dataLoaded: true
        });

        if (data.result.address == "") {
          $this.setData({
            myIf: true
          });
        }
        if (addressId) {
          ajax.G('/openapi/CAddress/getAddrById',
            {
              id: addressId
            },
            function (data) {
              var address = {};
              address.id = data.result.id;
              address.re_name = data.result.re_name;
              address.re_phone = data.result.re_phone;
              address.fullAddr = data.result.province_name + data.result.city_name + data.result.district_name + data.result.detail_addr;
              address.is_default = data.result.is_default;
              $this.setData({
                address: address,
                addressId: address.id
              })
            }
          )
        }
      }

    );
  },
  skip: function () {
    this.confirmGoods();
  },
  /**
   *支付 
   */
  confirmGoods: function () {
    var addressId = this.data.addressId;
    var goodsId = this.data.goodsId;
    var skuId = this.data.skuId;
    var amount = this.data.amount;
    var cartId = this.data.cartId;
    var orderId = this.data.orderId;
    var orderType = this.data.orderType;
    var $this = this;

    if (orderId) {
      this._payAgain();
      return;
    }
    ajax.P(
      '/openapi/CHome/quickOrder',
      {
        openId: app.getOpenId(),
        goods_id: goodsId,
        address_id: addressId,
        pay_type: 2,
        is_cash: 0,
        coupon_id: 0,
        sku_id: skuId,
        amount: amount,
        order_type: orderType,
        cartId: cartId,
      },
      function (data) {
        $this.setData({
          orderId: data.result.orderId,
        });

        $this._paySuccess(JSON.parse(data.result.wxPayParams));
      }
    )

  },

  /**
   * 再次支付
   */
  _payAgain() {
    var $this = this;

    ajax.P(
      '/openapi/CHome/doPayAgain',
      {
        openId: app.getOpenId(),
        orderId: this.data.orderId,
      },
      function (data) {
        $this._paySuccess(JSON.parse(data.result.wxPayParams));
      }
    )
  },

  /**
   * 支付成功回调
   */
  _paySuccess(wxPayParams) {
    wxPayParams.success = function () {
      wx.redirectTo({ url: '../orderList/orderList?ty=1' });
    }
    wxUtil.wxPay(wxPayParams);
  },
  /**
   * 选择地址
   */
  _goToAddr: function () {
    if (!this.data.myIf) {
      wx.redirectTo({
        url: '../../pages/myAddr/myAddr?goodsId=' + this.data.goodsId + '&skuId=' + this.data.skuId + '&amount=' + this.data.amount + '&cartId=' + this.data.cartId,
      })
    } else {
      wx.redirectTo({
        url: '../../pages/selectedAddr/selectedAddr?goodsId=' + this.data.goodsId + '&skuId=' + this.data.skuId + '&amount=' + this.data.amount + '&cartId=' + this.data.cartId,
      })
    }
  },


  /**
   * 页面加载
   */
  onLoad: function (options) {
    var _self = this;
    // 生命周期函数--监听页面加载
    if (options.cartId) {
      _self.setData({
        goodsId: JSON.parse(options.goodsId),
        skuId: JSON.parse(options.skuId),
        amount: JSON.parse(options.amount),
        cartId: JSON.parse(options.cartId),
        shop: app.getShop(),
      })
    } else {
      _self.setData({
        goodsId: JSON.parse(options.goodsId),
        skuId: JSON.parse(options.skuId),
        amount: JSON.parse(options.amount),
        shop: app.getShop(),
        cartId: 0
      })
    }
    if (options.addressId) {
      _self.setData({
        addressId: options.addressId
      });
      _self.showSalt(options.addressId);
    } else {
      _self.showSalt();
    }
  },

  onShareAppMessage: function () {
    return {
      title: this.data.shop.shareTitle,
      path: "pages/index/index"
    }
  },
})